package com.example.service;

import com.example.entity.User;
import com.example.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public Boolean add(User user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        //查询是否已存在此用户名
        User existingUser = userMapper.selectByUsername(user.getUsername());
        if(existingUser != null)
            return false;
        userMapper.insert(user);
        return true;
    }

    public User getUserByUsername(String username){
        User user = userMapper.selectByUsername(username);
        return user;
    }

    public User selectByPhoneOrEmail(String username){
        return userMapper.selectByPhoneOrEmail(username);
    }
}
